<?php

class Module
{
    function __construct()
    {
    }
	static $_instance = null;
	static function getInstance()
	{
		if( self::$_instance === null )
		{
			self::$_instance = new self();
		}
		return self::$_instance;
	}
    
    function getParam( $flag = null, $default = null )
    {
        $arr = array_merge( $_GET, $_POST );
        if( $flag === null )
        {
            $arr = $arr;
            $keys = array('m', 'a');
            foreach( $keys as $key )
            {
                unset( $arr[$key] );
            }
            return $arr;
        }
        
        if( ! isset( $arr[$flag] ) )
        {
            $arr[$flag] = $default ;
        }
        
        return $arr[$flag] ;
    }
    
    function display( $view )
    {
        $file = BASE_PATH.'/View/'.$view.'.html';
        if( is_file( $file ) )
        {
            echo file_get_contents( $file );
        }
        else
        {
            throw new Exception( "cant't find view [{$file}]." );
        }
    }
    
    function smartyOut( $view, $out = null )
    {
        $file = BASE_PATH.'/View/'.$view.'.html';
        if( ! is_file( $file ) )
        {
            throw new Exception("can't find [{$file}].");
        }
        require_once BASE_PATH.'/Library/Smarty/Smarty.class.php';        
        $smarty = Smarty::getInstance();
        $smarty->left_delimiter = '<!--{';
        $smarty->right_delimiter = '}-->';
        $smarty->assign( 'out', $out );
        $smarty->display( $file );
    }
    
    function info( $str, $preStr = null )
    {
        if( ! isset( $this->_date ) )
        {
            $this->_date = date( 'Y-m-d' );
        }
        
        if( is_array( $str ) )
        {
            $str = print_r( $str, true );
        }
        
        if( is_bool( $str ) )
        {
            $str = $str ? 'false' : 'true';
        }
        
        $file = BASE_PATH.'/log/'.$this->_date.'.txt';
        $pre = '['.date('H:i:s') . "]: " . $preStr;
        file_put_contents($file, $pre.$str."\n", FILE_APPEND);
    }
    
    function test()
    {
        echo "success.";
    }
}